package tests;

import common.Utils;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.text.SimpleDateFormat;
import java.util.List;

public class EditPage extends Utils {
    private static String URL = "http://8.137.19.140:9090/blog_edit.html";

    public EditPage() {
        super(URL);
    }

    /**
     * 发布博客 -- 输入标题和内容并提交
     * @throws InterruptedException
     */
    public void checkEditSuccess() throws InterruptedException {
        //生成动态标题
        SimpleDateFormat sim = new SimpleDateFormat("HH-mm-ss");
        String titleTime = sim.format(System.currentTimeMillis());
        String titleBefore = "发布博客 " + titleTime;
        driver.findElement(By.cssSelector("#title"));
        driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        driver.findElement(By.cssSelector("#submit"));
        WebElement title = driver.findElement(By.cssSelector("#title"));
        new Actions(driver).doubleClick(title).sendKeys("发布博客 " + titleTime).perform();

        WebElement context = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        new Actions(driver).click(context).sendKeys("自动化测试").perform();

        driver.findElement(By.cssSelector("#submit")).click();

        Thread.sleep(2000);
        List<WebElement> blogs = driver.findElements(By.cssSelector("body > div.container > div.right > div"));
        System.out.println(blogs.size());
        String titleAfter = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(" + blogs.size() + ") > div.title")).getText();
        assert titleBefore.equals(titleAfter);
    }

    /**
     * 发博客 - 标题为空 内容不为空
     */
    public void checkEditTitleIsNull() throws InterruptedException {
        driver.get(URL);
        WebElement context = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        new Actions(driver).doubleClick(context).sendKeys("自动化测试中").perform();
        String urlBefore = driver.getCurrentUrl();
        driver.findElement(By.cssSelector("#submit")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        Thread.sleep(1000);
        alert.accept();
        String urlAfter = driver.getCurrentUrl();
        //通过判断发布后的网址是否发生改变来判断是否发布成功
        assert urlBefore.equals(urlAfter);
    }
    public void checkEditContextIsNull() throws InterruptedException {
        SimpleDateFormat sim = new SimpleDateFormat("HH-mm-ss");
        String titleName = sim.format(System.currentTimeMillis());
        WebElement context = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        new Actions(driver).doubleClick(context).sendKeys(Keys.DELETE).perform();
        context = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
        new Actions(driver).doubleClick(context).sendKeys(Keys.DELETE).perform();
        WebElement title = driver.findElement(By.cssSelector("#title"));
        new Actions(driver).click(title).sendKeys("自动化测试中 " + titleName).perform();
        String urlBefore = driver.getCurrentUrl();
        driver.findElement(By.cssSelector("#submit")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        Thread.sleep(1000);
        alert.accept();
        String urlAfter = driver.getCurrentUrl();
        //通过判断发布后的网址是否发生改变来判断是否发布成功
        assert urlBefore.equals(urlAfter);

    }
}
